import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    },


    {
      path: '/home',
      name: 'home',
      component: HomeView,
      meta: { requiresAuth: true },
      children:[
        // 工作台路由
        {
          path: '/about',
          name: 'about',
          component: () => import('../views/AboutView.vue'),
          meta: { requiresAuth: true }
        },
        //测试路由
        {
          path: '/CS1',
          name: 'CS1',
          component: () => import('../views/CS/CS1.vue'),
          meta: { requiresAuth: true }
        },

        {
          path: '/Unified_page',
          name: 'Unified_page',
          component: () => import('../views/CS/Unified_page.vue'),
          meta: { requiresAuth: true }
        },

      ]
    },

        {
          path: '/Warning',
          name: 'Warning',
          component: () => import('../views/Warning.vue'),
          meta: { requiresAuth: true }
        },

         {
          path: '/Workshop',
          name: 'Workshop',
          component: () => import('../views/Workshop.vue'),
          meta: { requiresAuth: true }
        },
       {
          path: '/Progressboard',
          name: 'Progressboard',
          component: () => import('../views/Progressboard.vue'),
          meta: { requiresAuth: true }
        },
                {
          path: '/CS2',
          name: 'CS2',
          component: () => import('../views/CS/CS2.vue'),
          meta: { requiresAuth: true }
        },
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = sessionStorage.getItem('isLoggedIn') === 'true'
  
  if (to.meta.requiresAuth && !isLoggedIn) {
    next('/')
  } else if (to.path === '/' && isLoggedIn) {
    next('/home')
  } else {
    next()
  }
})

export default router
